﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BehaviorSystem
{
    /// <summary>
    /// 行为树节点
    /// </summary>
    public abstract class BehaviourNode : IBehaviourNode
    {
        public IBehaviorAgent BehaviourAgent { get;protected set; }

        private List<IBehaviourNode> childNodes = new List<IBehaviourNode>();

        public List<IBehaviourNode> ChildrenNodes { get { return childNodes; } }

        public BTNodeState CurState { get; protected set; }

        public bool IsNeedReDoPerTick { get; protected set; }


        public BehaviourNode(IBehaviorAgent agent)
        {
            BehaviourAgent = agent;
            CurState = BTNodeState.Running;
        }


        public void AddChildrenNode(IBehaviourNode childNode)
        {
            if (childNode.IsNeedReDoPerTick)
            {
                this.IsNeedReDoPerTick = true;
            }
            childNodes.Add(childNode);
        }

        public virtual void ResetNode()
        {

            CurState = BTNodeState.Running;
            for (int i = 0; i < ChildrenNodes.Count; i++)
            {
                ChildrenNodes[i].ResetNode();
            }
            OnReseted();
        }

        public virtual BTNodeState ExcuteNode()
        {
            OnExcuted();
            return CurState;
        }

        public virtual void OnExcuted()
        {

        }

        public virtual void OnReseted()
        {

        }

    }
}